﻿using System;
using System.Collections.Generic;
using System.Device.Location;

namespace ChinaGeo
{
    internal static class Extensions
    {
        public static GeoCoordinate Copy(this GeoCoordinate source)
        {
            if (source == null)
                return null;
            return new GeoCoordinate(source.Latitude, source.Longitude, source.Altitude, source.HorizontalAccuracy, source.VerticalAccuracy, source.Speed, source.Course);
        }

        public static ChinaGeoModel Copy(this ChinaGeoModel source)
        {
            if (source == null)
                return null;
            return new ChinaGeoModel(source.EarthGeo.Copy(), source.ChinaGeo.Copy(), source.Timestamp) { Cached = source.Cached };
        }

        public static TSource MinT<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector)
        {
            TSource t = default(TSource);
            double min = double.MaxValue;
            foreach (var item in source)
            {
                var val = selector(item);
                if (val < min)
                {
                    min = val;
                    t = item;
                }
            }
            return t;
        }
    }
}
